<?php

declare(strict_types=1);

namespace App\Admin;

use App\Entity\Gallery;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;

final class GalleryAdmin extends AbstractAdmin {
    protected $baseRouteName = 'admin_gallery';
    protected $baseRoutePattern = 'gallery';
    protected $accessMapping = [
        'image_upload' => 'IMAGE UPLOAD',
        'image_delete' => 'IMAGE DELETE',
        'image_sort'   => 'IMAGE SORT',
    ];

    public function toString ($object) {
        return $object instanceof Gallery ? $object->getOriginalName() : '图集';
    }

    protected function configureDatagridFilters (DatagridMapper $datagridMapper): void {
        $datagridMapper
            ->add('id')
            ->add('name')
            ->add('original_name')
            ->add('size')
            ->add('path')
            ->add('sort')
            ->add('admin_user_id')
            ->add('created_at')
            ->add('updated_at');
    }

    protected function configureListFields (ListMapper $listMapper): void {
        $listMapper
            ->add('id')
            ->add('name')
            ->add('original_name')
            ->add('size')
            ->add('path')
            ->add('sort')
            ->add('admin_user_id')
            ->add('created_at')
            ->add('updated_at')
            ->add('_action', null, [
                'actions' => [
                    'show'   => [],
                    'edit'   => [],
                    'delete' => [],
                ],
            ]);
    }

    protected function configureFormFields (FormMapper $formMapper): void {
        $formMapper
            ->add('id')
            ->add('name')
            ->add('original_name')
            ->add('size')
            ->add('path')
            ->add('sort')
            ->add('admin_user_id')
            ->add('created_at')
            ->add('updated_at');
    }

    protected function configureShowFields (ShowMapper $showMapper): void {
        $showMapper
            ->add('id')
            ->add('name')
            ->add('original_name')
            ->add('size')
            ->add('path')
            ->add('sort')
            ->add('admin_user_id')
            ->add('created_at')
            ->add('updated_at');
    }

    protected function configureRoutes (RouteCollection $collection) {
        parent::configureRoutes($collection); // TODO: Change the autogenerated stub

        $collection->remove('delete');
        $collection->remove('export');
        $collection->remove('edit');
        $collection->remove('show');
        $collection->remove('create');

        $collection->add('image_upload', 'image_upload');
        $collection->add('image_sort', 'image_sort');
        $collection->add('image_delete', 'image_delete/{id}', [], [], [], '', [], ['POST']);
    }

    public function getTemplate ($name) {
        switch ($name) {
            case 'list':
                return 'backend/gallery/list.html.twig';
            default:
                return $this->getTemplateRegistry()->getTemplate($name);
        }
    }
}
